package com.chijun.mform.field.impl;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.R;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.TimeUtils;
import com.github.gzuliyujiang.wheelpicker.DatimePicker;

import io.reactivex.ObservableEmitter;

/**
 * 日期
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:44
 */
public class FieldFor06RiQi extends BaseField {
    public FieldFor06RiQi(){
        super();
    }

    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {

        FormField formField = writeFormClass.formField;
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //是否只读
        if (formField.isReadOnly()) {
            writeFormClass.fieldFullContentView.setKeyListener(null);
            writeFormClass.fieldFullContentView.setFocusable(false);
        }

        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData == null) textFieldZanCunData = formField.getDefaultContent();
        //默认文本
        if (textFieldZanCunData != null) {
            try {
                long l = Long.parseLong(textFieldZanCunData);
                writeFormClass.fieldFullContentView.setText(TimeUtils.millis2String(l));
                saveValue = textFieldZanCunData;
            } catch (Exception e1) {
                long l = System.currentTimeMillis();
                writeFormClass.fieldFullContentView.setText(TimeUtils.millis2String(l));
                saveValue = String.valueOf(l);
            }
            writeFormClass.inputIsOK = true;
        }

        //文本改变事件
        writeFormClass.fieldFullContentView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                writeFormClass.inputIsOK = check(s,formField,writeFormClass.fieldFullWriteErrorView);
            }
        });

        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        writeFormClass.fieldFullJumpImgView.setVisibility(View.VISIBLE);
        writeFormClass.fieldFullJumpImgView.setImageResource(R.drawable.field_icon_riqixuanze);
        writeFormClass.fieldFullJumpImgView.setOnClickListener(v -> {
            DatimePicker datimePicker = new DatimePicker(ActivityUtils.getTopActivity());
            datimePicker.setOnDatimePickedListener((year, month, day, hour, minute, second) -> {
                String s = TimeUtils.formatDateTime(year, month, day, hour, minute, second, true);
                writeFormClass.fieldFullContentView.setText(s);
                long l = TimeUtils.string2Millis(s);
                saveValue = String.valueOf(l);
            });
            datimePicker.show();
        });
        return writeFormClass.view;
    }


    @SuppressLint("SetTextI18n")
    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();
        //设置字段值
        fullModel.loadContent.setKeyListener(null);
        fullModel.loadContent.setFocusable(false);
        fullModel.loadContent.setTextIsSelectable(true);//可选择

        String fieldValue = fullModel.serverFullData.formOrdinaryFieldDataJsonObject.getString(String.valueOf(fullModel.formField.getId()));
        if (StringUtils.isEmpty(fieldValue)) {
            try {
                long l = Long.parseLong(fieldValue);
                TimeUtils.millis2String(l);
                fullModel.loadContent.setText(TimeUtils.millis2String(l));
            } catch (Exception e) {
                LogUtils.e(e.getMessage());
                fullModel.loadContent.setText("加载错误" + e.getMessage());
                fullModel.loadContent.setTextColor(Color.RED);
            }
        }

        if (fullModel.isHaveModifyPower()) {
            fullModel.setJumpImageProperty(R.mipmap.form_field_date, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DatimePicker datimePicker = new DatimePicker(ActivityUtils.getTopActivity());
                    datimePicker.setOnDatimePickedListener((year, month, day, hour, minute, second) -> {
                        String s = TimeUtils.formatDateTime(year, month, day, hour, minute, second, true);
                        fullModel.loadContent.setText(s);
                        long l = TimeUtils.string2Millis(s);
                        processModifyFormFieldDataMap.put(fullModel.formField.getId(),String.valueOf(l));
                    });
                    datimePicker.show();
                }
            });
        }
        return fullModel.layout;
    }


    /**
     * 字段设置界面
     * @param context 上下文
     * @param installClass 配置
     * @return 1
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getTimeTypeConfigView());     //设置日期类型
        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印



        return installClass.v;
    }

}
